import random
import argparse


def generate_random_list(size, min_val=0, max_val=1000):
    """生成随机整数列表"""
    return [random.randint(min_val, max_val) for _ in range(size)]


def generate_sorted_list(size):
    """生成已排序的整数列表"""
    return list(range(size))


def generate_reverse_list(size):
    """生成逆序的整数列表"""
    return list(range(size, 0, -1))


def generate_few_unique_list(size, num_unique=10):
    """生成包含少量唯一值的大量重复数据"""
    return [random.randint(0, num_unique - 1) for _ in range(size)]


def save_to_file(data, filename):
    """将数据保存到文件"""
    with open(filename, 'w') as f:
        f.write(' '.join(map(str, data)))


def load_from_file(filename):
    """从文件加载数据"""
    with open(filename, 'r') as f:
        return list(map(int, f.read().split()))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Generate test data for sorting algorithms')
    parser.add_argument('--size', type=int, default=100, help='Size of the list to generate')
    parser.add_argument('--type', choices=['random', 'sorted', 'reverse', 'few'], default='random',
                        help='Type of data to generate')
    parser.add_argument('--output', type=str, default='test_data.txt', help='Output filename')

    args = parser.parse_args()

    if args.type == 'random':
        data = generate_random_list(args.size)
    elif args.type == 'sorted':
        data = generate_sorted_list(args.size)
    elif args.type == 'reverse':
        data = generate_reverse_list(args.size)
    elif args.type == 'few':
        data = generate_few_unique_list(args.size)

    save_to_file(data, args.output)
    print(f"Generated {len(data)} elements and saved to {args.output}")